package com.example.lesson;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView image;
    Button btn_last,btn_next;
    private  int[] imgs={
            R.mipmap.fore,
            R.mipmap.one,
            R.mipmap.three,
    };
    int index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image=findViewById(R.id.img);
        btn_last=findViewById(R.id.btn_last);
        btn_next=findViewById(R.id.btn_next);
        image.setImageResource(imgs[index]);
        btn_last.setOnClickListener(new mClick());
        btn_next.setOnClickListener(new mClick());
    }
    class mClick implements OnClickListener{
        public void onClick(View v){
            if(v == btn_last){
                if(index>0 && index<imgs.length){
                    image.setImageResource(imgs[--index]);
                }else{
                    index=imgs.length-1;
                    image.setImageResource(imgs[index]);
                }
            }
            if(v == btn_next){
                if(index>=0 && index<imgs.length-1){
                    image.setImageResource(imgs[++index]);
                }else{
                    index=0;
                    image.setImageResource(imgs[0]);
                }
            }
        }
    }
}


